package common.post.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import common.customer.bean.Customer;
import common.post.bean.Contacts;
import common.post.bean.Post;
import common.post.bean.SInfo;
import common.post.dao.ContactsDao;
import common.post.dao.PostDao;
import common.post.dao.SInfoDao;
import common.system.bean.City;
import common.system.dao.AreaDao;
import common.util.CommonCode;

public class PostServiceImpl implements PostService {
	private PostDao postDao;
	private ContactsDao contactsDao;
	private SInfoDao sInfoDao;
	private AreaDao areaDao;
	@Override
	public List<Post> getPostByType(String postType,int cityId,int page,int pageSize) {
		String queryStr = " from common.post.bean.Post p where p.city.id="+cityId+" and p.type='"+postType+"'";
		return postDao.find(queryStr,page,pageSize);
	}
	@Override
	public long countPostByType(String postType,int cityId) {
		String queryStr = " select count(p) from common.post.bean.Post p where p.city.id="+cityId+" and p.type='"+postType+"'";
		return (Long)postDao.find(queryStr,true).get(0);
	}
	@Override
	public Post getPostById(long postId) {
		Post post = this.postDao.get(postId);
		post.setClick((post.getClick()+1));
		this.postDao.update(post);
		return post;
	}
	@Override
	public List<Post> getGonggaoByTypeAndChannel(String postType, String channel) {
		String queryStr = " from common.post.bean.Post p where (p.channel='"+channel+"' or p.channel='"+CommonCode.SYSTEM_CHANNEL_ALL+"')  and p.type='"+postType+"'";
		System.out.println("======xian or and======"+queryStr);
		return postDao.find(queryStr);//最多5条公告
	}
	@Override
	public long addPost(Post post) {
		long id = this.postDao.save(post);
		return id;
	}
	@Override
	public int publishValidationPost(String postType, String sinfoType,
			String postTitle,String phone, String qq, String qqName, String areaId,
			String desc, String desc2,String channel,City city,Customer user) {
		List<Contacts> list = this.contactsDao.getByContent(qq);
		Contacts c_qq = list.size()<=0?null:list.get(0);
		if(c_qq==null){
			SInfo sinfo =null;
			c_qq = new Contacts();
			c_qq.setContent(qq);
			c_qq.setDesc("联系人QQ");
			c_qq.setTypeName(CommonCode.CONNTCTS_TYPE_QQ);
			int id = this.contactsDao.save(c_qq);
			if(id<=0){
				throw new RuntimeException("存联系人QQ出错");
			}
			List<Contacts> list2 = this.contactsDao.getByContent(phone);
			Contacts c_phone = list2.size()<=0?null:list2.get(0);
			if(c_phone==null){
				
				c_phone = new Contacts();
				c_phone.setContent(phone);
				c_phone.setDesc("联系人 手机");
				c_phone.setTypeName(CommonCode.CONNTCTS_TYPE_TELPHONE);
				id = this.contactsDao.save(c_phone);
				if(id<=0){
					throw new RuntimeException("存联系人手机出错");
				}
				sinfo = new SInfo();
				sinfo.setArea(areaDao.get(Integer.parseInt(areaId)));
				sinfo.setChaping(0);
				sinfo.setCity(city);
				Set<Contacts> set = new HashSet<Contacts>();
				set.add(c_phone);
				set.add(c_qq);
				sinfo.setContacts(set);
				sinfo.setContent(desc);
				sinfo.setHaoping(0);
				sinfo.setNiming(false);
				//sinfo.setPics(pics);
				//sinfo.setPublishDate()
				sinfo.setPublisher(user);
				sinfo.setPublishTime(System.currentTimeMillis());
				sinfo.setTitle(qqName);
				sinfo.setTuijian(false);
				sinfo.setType(sinfoType);
				sinfo.setZhongping(0);
				sinfo.setVip(false);
			}else{
				sinfo = c_phone.getSinfo();
			}
			this.sInfoDao.saveOrUpdate(sinfo);
			
			Post post = new Post();
			post.setChannel(channel);
			post.setCity(city);
			post.setContent(desc2);
			post.setCustomer(user);
			post.setInfo(sinfo);
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			
			this.sInfoDao.update(sinfo);
			long id2 = this.postDao.save(post);
			if(id2>0){
				return sinfo.getId();
			}else{
				throw new RuntimeException("新增联系方式出错");
			}
		}else{
			/*List<Contacts> list2 = this.contactsDao.find("from Contacts c where c.content='"+phone+"'");
			Contacts c_phone = list2.size()>0?null:list2.get(0);*/
			Contacts c_phone = null;
			boolean isIn = false;
			SInfo sinfo = c_qq.getSinfo(); 
			for(Contacts c:c_qq.getSinfo().getContacts()){
				if(phone.trim().equals(c.getContent())){
					isIn=true;
				}
			}
			if(isIn){
				
			}else{
				c_phone = new Contacts();
				c_phone.setContent(phone);
				c_phone.setDesc("联系人手机");
				c_phone.setTypeName(CommonCode.CONNTCTS_TYPE_TELPHONE);
				int id = contactsDao.save(c_phone);
				if(id<=0){
					throw new RuntimeException("新增联系方式出错");
				}
				sinfo.getContacts().add(c_phone);
			}
			sinfo.setContent(sinfo.getContent()+"<br/>由"+user.getUserName()+"&nbsp;&nbsp;&nbsp;补充<br>"+desc);
			Post post = new Post();
			post.setChannel(channel);
			post.setCity(city);
			post.setContent(desc2);
			post.setCustomer(user);
			post.setInfo(sinfo);
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			
			this.sInfoDao.update(sinfo);
			long id = this.postDao.save(post);
			if(id>0){
				return sinfo.getId();
			}else{
				throw new RuntimeException("存储帖子信息出错");
			}
		}
	}
	
	//==============getter and setter method================
	public PostDao getPostDao() {
		return postDao;
	}
	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}
	public ContactsDao getContactsDao() {
		return contactsDao;
	}
	public void setContactsDao(ContactsDao contactsDao) {
		this.contactsDao = contactsDao;
	}
	public SInfoDao getsInfoDao() {
		return sInfoDao;
	}
	public void setsInfoDao(SInfoDao sInfoDao) {
		this.sInfoDao = sInfoDao;
	}
	public AreaDao getAreaDao() {
		return areaDao;
	}
	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}
	
	
}
